package com.techshine.util;

import java.io.UnsupportedEncodingException;

import com.alibaba.druid.pool.vendor.NullExceptionSorter;

/**
 * 
 *
 * @Description 
 * @author (作者) liukd
 * @date (开发日期) 2016年10月26日 下午2:13:04 
 * @company (开发公司) 北京精仪达盛科技有限公司
 * @copyright (版权) 本文件归北京精仪达盛科技有限公司所有
 * @version (版本) V1.0
 * @since (该版本支持的JDK版本) ： 1.7
 * @modify (修改) 
 *                  
 *                  
 *                  
 * @Review (审核人)
 */
public class StringUtil {
    
    /**
     * 
     * @Description 判断对象是否为空
     * @author liukd
     * @date 2016年10月26日 下午2:13:11 
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj) {
        return obj == null || "".equals(obj);
    }
    
    /**
     * 
     * @Description 判断对象是否不为空
     * @author liukd
     * @date 2016年10月26日 下午2:53:15 
     * @param obj
     * @return
     */
    public static boolean isNotEmpty(Object obj) {
        return obj != null && !"".equals(obj);
    }
    public static boolean isNotEmptyDeTrim(Object obj) {
    	if(obj != null && !"".equals(obj)){
    		String a =  obj.toString().trim();
    		return a != null && !"".equals(a);
    	}
        return false;
    }
    /**
     * 
     * @Description 首字母小写
     * @author liukd
     * @date 2016年10月26日 下午2:13:17 
     * @param name
     * @return
     */
    public static String toLowerFirstName(String name) {
        if (isNotEmpty(name)) {
            return name.substring(0, 1).toLowerCase() + name.substring(1);
        }
        else {
            return "";
        }
    }
    
    /**
     * 
     * @Description 获得action小写名称 
     * @author liukd
     * @date 2016年10月26日 下午2:13:25 
     * @param actionName
     * @return
     */
    public static String getLowerActionName(String actionName) {
        String action = "";
        if (isNotEmpty(actionName)) {
            int index = actionName.indexOf("Action");
            if (index != -1) {
                action = actionName.substring(0, index);
            }
            action = toLowerFirstName(action);
        }
        return action;
    }
    
    public static String formatString(String str, int length, String symbol) {
        int strLen = str.length();
        if (strLen < length) {
            while (strLen < length) {
                StringBuffer sb = new StringBuffer();
                sb.append(str).append(symbol);// 左补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }
    
    public static String subStringByByte(String str, int n) {
        int num = 0;
        try {
            byte[] buf = str.getBytes("GBK");
            if (n >= buf.length) {
                return str;
            }
            boolean bChineseFirstHalf = false;
            for (int i = 0; i < n; i++) {
                if (buf[i] < 0 && !bChineseFirstHalf) {
                    bChineseFirstHalf = true;
                }
                else {
                    num++;
                    bChineseFirstHalf = false;
                }
            }
        }
        catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str.substring(0, num);
    }
    
    /**
     * 将以逗号分隔的字符串转换成字符串数组
     * @param valStr
     * @return String[]
     */
    public static String[] strList(String valStr) {
    	String[] returnStr ;
    	
    	if(",".indexOf(valStr) > 0){
    		int i = 0;
            String tempStr = valStr;
            returnStr = new String[valStr.length() + 1 - tempStr.replace(",", "").length()];
            valStr = valStr + ",";
            while (valStr.indexOf(',') > 0) {
                returnStr[i] = valStr.substring(0, valStr.indexOf(','));
                valStr = valStr.substring(valStr.indexOf(',') + 1, valStr.length());
                
                i++;
            }
    	}else{
    		returnStr =  new String[1];
    		returnStr[0] = valStr;
    	}
    	return returnStr;
    }
    
    
    
    
    
    
    /**
     * ueditor上传的 标签 得到sysFileId
     * @param p
     * @return
     */
    public static String pToStringId(String p){
         if(!StringUtil.isNotEmpty(p)){
        	 return "";
         }
         
         if(!p.contains("<")){
        	 return p;
         }
         	
         String sysFileId = "";
         	try{
         		/**
             	 * swf 文件路径 在线预览
             	 * <p style="line-height: 16px;"><img src="http://localhost/hrbust_lab_platform/js/ueditor/dialogs/attachment/fileTypeImages/icon_txt.gif"/><a style="font-size:12px; color:#0066cc;" href="file.action?id=38ba12cf947a4aac9a732746f67e6be5&swf=upload/jsp/upload/file/20180103/1514964654074080642.swf&sysFileId=38ba12cf947a4aac9a732746f67e6be5" title="1212问题总结(1).docx">1212问题总结(1).docx</a></p><p><br/></p>
             	 */
         		int indexSwf = p.indexOf("&swf=");
         		
         		
         		int indexSysFileId = p.indexOf("&sysFileId");
         		
         		if(indexSwf > 0 && indexSysFileId > 0 && indexSwf <= indexSysFileId){
         			String href = p.substring(indexSwf+5,indexSysFileId);
                 	
                 	/**
                 	 * sysFileId  级联删除等
                 	 */
                    sysFileId = p.substring(indexSysFileId+11,indexSysFileId+11+32);
         		}
         		
         	}catch(Exception e){
         		e.printStackTrace();
         	}
         	
         	return sysFileId;
         	
         	
    }
    
    
    public static String pToStringSwf(String p){
        if(!StringUtil.isNotEmpty(p)){
       	 return "";
        }
        
        if(!p.contains("<")){
       	 return p;
        }
        	
        String sysFileId = "";
        String swf = "";
        	try{
        		/**
            	 * swf 文件路径 在线预览
            	 * <p style="line-height: 16px;"><img src="http://localhost/hrbust_lab_platform/js/ueditor/dialogs/attachment/fileTypeImages/icon_txt.gif"/><a style="font-size:12px; color:#0066cc;" href="file.action?id=38ba12cf947a4aac9a732746f67e6be5&swf=upload/jsp/upload/file/20180103/1514964654074080642.swf&sysFileId=38ba12cf947a4aac9a732746f67e6be5" title="1212问题总结(1).docx">1212问题总结(1).docx</a></p><p><br/></p>
            	 */
        		int indexSwf = p.indexOf("&swf=");
        		
        		
        		int indexSysFileId = p.indexOf("&sysFileId");
        		
        		if(indexSwf > 0 && indexSysFileId > 0 && indexSwf <= indexSysFileId){
        			String href = p.substring(indexSwf+5,indexSysFileId);
                	//href="file.action?id=38ba12cf947a4aac9a732746f67e6be5&swf=upload/jsp/upload/file/20180103/1514964654074080642.swf&sysFileId=38ba12cf947a4aac9a732746f67e6be5"
                	/**
                	 * sysFileId  级联删除等
                	 */
                   sysFileId = p.substring(indexSysFileId+11,indexSysFileId+11+32);
                   swf = p.substring(indexSwf+5,indexSysFileId);
        		}
        		
        	}catch(Exception e){
        		e.printStackTrace();
        	}
        	
        	return swf;
        	
        	
   }
    
    
    
    public static void main(String[] args) {
		String p = "<p style=\"line-height: 16px;\"><img src=\"http://localhost/hrbust_lab_platform/js/ueditor/dialogs/attachment/fileTypeImages/icon_txt.gif\"/><a style=\"font-size:12px; color:#0066cc;\" href=\"file.action?id=38ba12cf947a4aac9a732746f67e6be5&swf=upload/jsp/upload/file/20180103/1514964654074080642.swf&sysFileId=38ba12cf947a4aac9a732746f67e6be5\" title=\"1212问题总结(1).docx\">1212问题总结(1).docx</a></p><p><br/></p>";
			
			String swf = StringUtil.pToStringSwf(p);
			
			System.out.println(swf);
			
	}
    
    private boolean isNotEmpty = true;
    public  StringUtil chainEmpty(Object obj)throws Exception{
    	if(isEmpty(obj)){
    		isNotEmpty = false;
    	}
    	
    	return this;
    }
	public boolean isNotEmpty() {
		return isNotEmpty;
	}
    
    
    
    
    
    
}
